package com.qxglxt.qxgl.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.qxglxt.qxgl.dao.IUsersDao;
import com.qxglxt.qxgl.dto.UsersDTO;
import com.qxglxt.qxgl.model.Users;
import com.qxglxt.qxgl.service.IUsersService;

import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

public class UsersController {

    @Resource
    private IUsersDao usersDao;
    @Resource
    private IUsersService usersService;


    @ApiOperation(value = "查询所有用户",notes = "查询所有用户")
    @GetMapping
    public Map<String ,Object> findAll(UsersDTO dto){
        Page<Users> page=usersService.findAllPage(dto);
       
        Map<String,Object> map=new HashMap<String ,Object>();
        map.put("code", 0);
        map.put("msg", "成功");
        map.put("count", page.getNumberOfElements());
        map.put("data", page.getContent());
        return map;
    }

     
    @ApiOperation(value="添加用户",notes="添加用户")
    @PostMapping()
    public Users add(@RequestBody Users u){
        return usersDao.save(u);
    }

    @ApiOperation(value = "根据编号删除用户",notes="根据编号删除用户")
    @ApiImplicitParam(name="id",value ="用户编号",required=true,paramType ="path")
    @DeleteMapping(value ="/{id}")
      public void del(@PathVariable Integer id) {
          
        usersDao.deleteById(id);

      }

      @ApiOperation(value="根据编号修改用户信息",notes = "根据编号修改用户信息")
	  @ApiImplicitParams({
		  @ApiImplicitParam(name="Id",value ="用户编号",required=true,paramType ="path"),
		  @ApiImplicitParam(name="LoginName",value ="新的登录名",required=true,paramType ="path"),
		  @ApiImplicitParam(name="ProtectEMail",value ="新的邮箱",required=true,paramType ="path"),
		  @ApiImplicitParam(name="ProtectMTel",value ="新的电话",required=true,paramType ="path")
		 
		  
	  })
	  @PutMapping(value = "/edit/{Id}/{LoginName}/{ProtectEMail}/{ProtectMTel}")
	  public int editMember(@PathVariable Integer Id,@PathVariable String LoginName,@PathVariable String ProtectEMail,@PathVariable String ProtectMTel) {
		  
		return usersDao.editUsers(Id, LoginName, ProtectEMail, ProtectMTel);
		  
      }

      @ApiOperation(value="锁定用户",notes = "锁定用户")
      @PutMapping
      public int lockUser(@PathVariable Integer Id){
    
        return usersDao.lockoutUser(Id);
      }
    
      @ApiOperation(value="解锁用户",notes = "解锁用户")
      @PutMapping(value = "/un")
      public int unLockUser(@PathVariable Integer Id){
    
        return usersDao.notLockoutUser(Id);
      }
    
      @ApiOperation(value = "登录",notes = "登录")
      @ApiImplicitParams({
        @ApiImplicitParam(name="LoginName",value ="登录名",required=true,paramType ="path"),
        @ApiImplicitParam(name="Password",value ="密码",required=true,paramType ="path")
       
        
    })
      @GetMapping(value = "/login")
      public int login(@PathVariable String LoginName,@PathVariable String Password){
    
    
       
    
       return usersDao.login(LoginName, Password);
    
      }  


}